<template>
	<view class="mine-page">
		<!-- 顶部导航栏 -->
		<shopro-navbar :isBack="false" :background="{background: '#f5f7fa'}">
			<text slot="content" class="u-flex u-flex-center u-flex-items-center" style="color: #000; width: 100%; text-align: center;padding-left: 20rpx;height: 100%;">个人中心</text>
		</shopro-navbar>
		<view class="user-info-content">
			<!-- 用户信息区：登录/未登录切换 -->
			<view class="user-info" :class="{ clickable: !userInfo }" @click="!userInfo && goLogin()">
				<image class="avatar" :src="userInfo ? (userInfo.avatar || HTTP_IMG_URL()+'/static/images/avater.png') : HTTP_IMG_URL()+'/static/images/avater.png'" mode="aspectFill" />
				<view class="user-meta">
					<view v-if="userInfo" class="u-flex  u-flex-between u-flex-items-center">
						<view class="u-flex u-flex-column">
							<view class="row">
								<text class="nickname">
									{{ userInfo.nickname || userInfo.username || '用户' }}
								</text>
								<view v-if="userInfo.levelName" class="tag">
									{{ userInfo.levelName }}
								</view>
							</view>
							<text class="meta-text">
								ID：{{ userInfo.id || userInfo.uid || '--' }}
							</text>
							<text class="meta-text">
								账号：{{ userInfo.account || userInfo.mobile || '--' }}
							</text>
						</view>
						<view class="capacity-btn" type="primary" size="mini" shape="circle">学员</view>
					</view>
					<view v-else>
						<text class="nickname">未登录</text>
					</view>
				</view>
			</view>
	
			<!-- 购买课程横条 -->
			<view class="buy-bar">
				<text class="buy-title">购买课程</text>
				<view class="look-btn" type="primary" size="mini" shape="circle" @click.stop="goCourse">立即查看</view>
			</view>
		</view>

		<!-- 功能区宫格 -->
		<view class="menu-section">
			<u-grid :col="4" :border="false">
				<u-grid-item v-for="(item, idx) in menuList1" :key="item.text" @click="onMenuClick(item)">
					<image :src="item.img" class="menu-img" mode="aspectFill"> </image>
					<text class="menu-text">{{ item.text }}</text>
				</u-grid-item>
			</u-grid>
			
		</view>
		
		<view class="menu-section">
			<u-grid :col="4" :border="false" customStyle="margin-top: 24rpx;">
				<u-grid-item v-for="(item, idx) in menuList2" :key="item.text" @click="onMenuClick(item)">
					<image :src="item.img" class="menu-img2" mode="aspectFill"> </image>
					<text class="menu-text">{{ item.text }}</text>
				</u-grid-item>
			</u-grid>
		</view>
	</view>
</template>

<script>
import { mapGetters } from "vuex";
import recordApi from '@/api/record.js'
export default {
	data() {
		return {
			userInfo: {},
			menuList1: [
				{
					text: "我的记录",
					img: this.$imgUrl+"/static/images/caozuojilu.png",
					url: "/pages/record/myRecords",
				},
				{
					text: "训练预约",
					img: this.$imgUrl+"/static/images/yuyue.png",
					url: "/pages/xunlian/xunlian",
				},
				{
					text: "开始做题",
					img: this.$imgUrl+"/static/images/zuoti.png",
					url: "/pages/questionnaire/zuoti",
				},
				{
					text: "错题本",
					img: this.$imgUrl+"/static/images/cuoti.png",
					url: "/pages/questionnaire/wrongQuestionnaire",
				},
			],
			menuList2: [
				{
					text: "基本信息",
					img: this.$imgUrl+"/static/images/jibenxinxi.png",
					url: "/pages/mine/myInfo",
				},
				{
					text: "我的订单",
					img: this.$imgUrl+"/static/images/dingdan.png",
					url: "/pages/pay/orderList",
				},
				{
					text: "关于我们",
					img: this.$imgUrl+"/static/images/guanyuwomen.png",
					url: "/pages/mine/aboutUs",
				},
				{
					text: "退出登录",
					img: this.$imgUrl+"/static/images/tuichu.png",
					url: "",
				}
			],
		}
	},
	onShow() {
		const token = wx.getStorageSync('LOGIN_STATUS_TOKEN');
		if(token) {
			this.userInfo = JSON.parse(wx.getStorageSync('USER_INFO'));
		} else {
			this.goLogin();
		}
	},
	methods: {
		getUserInfo() {
			recordApi.getUserInfo().then((res) => {
				if (res.response.data.code == 1) {
					this.userInfo = res.response.data.data.userinfo;
					console.log(res.response.data.data.userinfo, 'userInfo');
				}
			})
		},
		HTTP_IMG_URL() {
			return this.$imgUrl;
		},
		goLogin() {
			uni.navigateTo({
				url: "/pages/login/login",
			});
		},
		goCourse() {
			uni.navigateTo({
				url: "/pages/record/courseRecord",
			});
		},
		onMenuClick(item) {
			if (item.text === "退出登录") {
				// 退出登录逻辑
				uni.showModal({
					title: "提示",
					content: "确定要退出登录吗？",
					success: (res) => {
						if (res.confirm) {
							this.$store.commit("LOGOUT");
							uni.showToast({ title: "已退出登录", icon: "none" });
							setTimeout(() => {
								uni.navigateTo({ url: "/pages/login/login" });
							}, 500);
						}
					},
				});
				return;
			}
			if (item.url) {
				uni.navigateTo({
					url: item.url,
				});
			}
		},
	}
}
</script>

<style scoped lang="scss">
	.mine-page {
		background: #f7f8fa;
		min-height: 100vh;
		padding-bottom: 40rpx;
	}
	.navbar-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #222;
	}
	.user-info-content{
		background:  linear-gradient( 180deg, #FDFDFD 0%, #E2E8F8 100%);
	}
	.user-info {
		display: flex;
		align-items: center;
		border-radius: 20rpx;
		margin: 32rpx 24rpx 0 24rpx;
		padding: 32rpx 24rpx;
		box-shadow: 0 2rpx 8rpx #f0f1f2;
		/* 仅未登录时可点击 */
	}
	.user-info.clickable {
		cursor: pointer;
	}
	.avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		background: #f3f4f6;
		margin-right: 24rpx;
	}
	.user-meta {
		width: calc(100% - 100rpx);
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
	.nickname {
		font-size: 32rpx;
		font-weight: 500;
		color: #222;
	}
	.row {
		display: flex;
		align-items: center;
	}
	.tag {
		margin-left: 16rpx;
		background: linear-gradient(90deg, #6db6ff 0%, #3a8dff 100%);
		color: #fff;
		font-size: 22rpx;
		border-radius: 24rpx;
		padding: 2rpx 18rpx;
		font-weight: 500;
	}
	.meta-text {
		font-size: 24rpx;
		color: #888;
		margin-top: 6rpx;
	}
	.capacity-btn{
		background: rgba(22,100,255,0.15);
		border-radius: 16px;
		color: #1664FF;
		padding: 10rpx 40rpx;
	}
	.buy-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: linear-gradient( 90deg, #93A4C4 0%, #53668B 100%);
		border-radius: 16px 16px 0px 0px;
		margin: 24rpx 24rpx 0 24rpx;
		padding: 24rpx 32rpx;
		color: #fff;
		.buy-title {
			font-size: 30rpx;
			font-weight: 600;
		}
		.look-btn{
			background: linear-gradient( 90deg, #7784A5 0%, #6D7C9C 100%);
			border-radius: 16px;
			padding: 10rpx 20rpx;
			font-size:24rpx;
		}
	}
	
	.menu-section {
		background: #fff;
		border-radius: 20rpx;
		margin: 24rpx 24rpx 0 24rpx;
		padding: 32rpx 0 24rpx 0;
		box-shadow: 0 2rpx 8rpx #f0f1f2;
	}
	.menu-text {
		display: block;
		margin-top: 12rpx;
		font-size: 26rpx;
		color: #222;
	}
	.menu-img{
		width: 40rpx;
		height: 40rpx;
	}
	.menu-img2{
		width: 50rpx;
		height: 50rpx;
	}
</style>